package org.example.day11;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 4.1.13.2 lock.tryLock() 尝试获取锁，如果获取到返回true，如果没有返回false.(获取不到锁不会等待，直接向下继续执行.)
 **/
public class MyThread16 {
    public ReentrantLock lock = new ReentrantLock();
    public void waitMethod(){
        if(lock.tryLock()){
            System.out.println(Thread.currentThread().getName() + "获得锁");
            System.out.println(lock.isLocked());
            System.out.println(lock.isHeldByCurrentThread() + " ThreadName = " + Thread.currentThread().getName());
        }else {
            System.out.println(Thread.currentThread().getName() + "没有获得锁");
        }
    }

    public static void main(String[] args) {
        final MyThread16 m16 = new MyThread16();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                m16.waitMethod();
            }
        };
        Thread ta = new Thread(runnable);
        ta.setName("A");
        ta.start();
        Thread tb = new Thread(runnable);
        tb.setName("B");
        tb.start();
    }
}
